$('.modal').on('shown.bs.modal', function (e) {
    $(this).find('.modal-dialog').css({
        'margin-top': function () {
            var modal_height = $(this).height();
            return ($(window).height() / 2 - (modal_height / 2));
        }
    });
});


jQuery.dialog = {
    dlg: null,
    alert: function(msg, okHandle) {
    	swal({
    		text: msg,
    		confirmButtonText: '确认'
    	}).then(function() {
    		if(okHandle) okHandle();
    	})
    },
    confirm: function(msg, okHandle, dismissHandle) {
    	swal({
    		text: msg,
    		buttons: ["取消", "确认"],
    	}).then(function(willDelete) {
    		if(willDelete) {
    			if(okHandle) okHandle();
    		} else {
    			if(dismissHandle) dismissHandle(dismiss);
    		}
    	})
    }
};


jQuery.extend({ 
    /**
     * 页面布局函数, 例如：
     * $.layout(function(){
     *      var height = $(window).height();
     *      $("#menu_tree").slimScroll({"height": height - 240 < 464 ? 464 : height - 240});
     *       $("#menu_option").slimScroll({"height": height - 504 < 200 ? 200 : height - 504});
     *      $("#menu_info").css("height", 200);
     * });
     *
     * @param {function} fun 布局函数
     * @returns 无
     */       
    doLayout: function(fun) {
        fun();
        $(window).resize(function(){
            fun();
        });
    },
    /**
     * 数字填零补位
     */
    numberPad: function(num, n) {  
        var len = num.toString().length;  
        while(len < n) {  
            num = "0" + num;  
            len ++;  
        }  
        return num;  
    }
});

(function($) {
    /**
     * 设置表单控件值
     * 
     * @param {object} data json对象
     */
    $.fn.setValues = function(data) { 
        if(data == null) return this;
        return this.each(function() {
            $(this).find('input, textarea, select').each(function() {
                var name = $(this).attr('name');
                var arr = name.split('.');
                if(arr.length == 1) {
                    if(data[name] != undefined) {
                    	if($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
                    		if($(this).val() == data[name]) {
                    			$(this).attr("checked",true);
                    		} else {
                    			$(this).attr("checked",false);
                    		}
                    	} else {
                    		$(this).val(data[name]);	
                    	}
                    }
                } else { //类似 dept.id 等情况 
                	var subData =data;
                	arr.forEach(function(item, index) {
                		if(subData != undefined) {
                			subData = subData[item];
                		}
                	})
                	if(subData != undefined) {
                		if($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
                    		if($(this).val() == subData) {
                    			$(this).attr("checked",true);
                    		} else {
                    			$(this).attr("checked",false);
                    		}
                    	} else {
                    		$(this).val(subData);	
                    	}
                	}
                }
            })
        });
    };

    /**
     * 初始化表单, 去除验证信息
     * 
     * @param {object} validate jQuery Validate 对象
     */
    $.fn.initForm = function(validate, data) {
        $(this)[0].reset();
        $(this).find("[type='hidden']").removeAttr('value');
        if(validate) validate.resetForm();
        $(this).find(".error").removeClass("error");
        if(data) $(this).setValues(data);
    }
    /**
     * 将表单数据序列化为对象
     * 例：
     * 			
	$.ajax({
		type: "POST",
		url: "[[@{/meeting/test}]]",
		data: JSON.stringify($("#meeting_form").serializeObject()),
		contentType : "application/json",
		dataType : 'json',
		success: function(ret){
			$.dialog.alert(ret.message, function(){
				if(ret.success) 
			});
			
		}
	});
     */
    $.fn.serializeObject = function() {  
        var o = {};  
        var a = this.serializeArray();  
        $.each(a, function() {  
            if (o[this.name]) {  
                if (!o[this.name].push) {  
                    o[this.name] = [ o[this.name] ];  
                }  
                o[this.name].push(this.value || '');  
            } else {  
                o[this.name] = this.value || '';  
            }  
        });  
        return o;  
    }  
})(jQuery);      